<?php
namespace Wap\Controller;
use Common\Model\ArticleModel;
use Common\Model\CollectModel;
use Common\Model\FollowModel;
use Common\Model\NoticeModel;
use Common\Model\UtilsModel;
use Common\Model\EventsModel;
use Wap\BaseController;
use Common\Model\UserModel;
class UserController extends BaseController {

    public function index(){
        $uid = session('uid');
        if(!$uid) redirect(U('User/login')."&uri=".urlencode($_SERVER['REQUEST_URI']));

        //设置最后进来时间// 距离上次三分钟进来一次 更新一下用户信息(比如 积分 等)
        if(session('lastJoinTime') && session('lastJoinTime')<(time()-180) ){
            $data = UserModel::getUser($uid);
            self::resetUserSessions($data['data']);
            session('lastJoinTime',time());
        }

        $this->smarty->assign('signIsON',USER_HOME_MYSIGN_ENABLE);//我的签到是否开启

        signAssignWap($uid,$this->smarty,2);

        //查询用户信息完善状态
        $status = UserModel::isUserInfoPerfect($uid,OPT_INFO_PERFECT);
        $this->smarty->assign('isInfoPerfect',$status);
        $newNotice = session('newNotice');//NoticeModel::checkRedPrompt($uid);
        $this->smarty->assign('new',$newNotice);
        $this->smarty->assign('title','我');

        $eventPhoto=EventsModel::getEventsPhoto(0,$uid,1);
        if(count($eventPhoto)>0){
            $this->smarty->assign('hasPhoto',1);
        }else{
            $this->smarty->assign('hasPhoto',0);
        }

        $this->smarty->display("User_v2/index.html");
    }

    /**
     * 用户登录
     * User: Rick.Sun.<sunwgjj@126.com>
     * Return: void
     * Address: 浙江宣逸网络科技有限公司
     */
    public function login(){
        $url = I("get.uri",null,'urldecode');
        $url = $url ? $url : $_SERVER['HTTP_REFERER'];
        if(session('uid')) redirect(U('User/index'));
        $this->smarty->assign('url',$url);
        $this->smarty->assign('title','帐号登录');
        $this->smarty->display("User_v2/login.html");
    }

    
    //登陆
    public function signIn(){
        $phone = I("get.phone", null, "strip_tags");
        $password = I("get.password", null, "strip_tags");

        $data = UserModel::getUserByPhoneAndPassword($phone, $password,CLIENT_FROM_WAP);

        if($data['status'] == 0){
            self::resetUserSessions($data['data']);
        }
        $this->ajaxReturn($data);
    }

    //登出
    public function logout(){
        $_SESSION = array();
        unset($_SESSION);
        session_destroy();
        redirect(U("Wap/index/index"));
    }

    //注册
    public function reg(){
        if(session('uid')) redirect(U('User/index'));
        $this->smarty->assign('title','用户注册');
        $this->smarty->display("User_v2/reg.html");
    }
    //注册
    public function register(){
        $phone = I("get.phone", null, "strip_tags");
        $code = I("get.code", null, "strip_tags");
        $password = I("get.password", null, "strip_tags");
        $nickname = I("get.nickname", null, "strip_tags");

        $data = UserModel::registerUserByPhone($phone, $password, $nickname, $code,CLIENT_FROM_WAP);
        if($data['status'] == 0){
            self::resetUserSessions($data['data']);
        }
        $this->ajaxReturn($data);
    }

    //忘记密码
    public function forget(){
        if(session('uid')) redirect(U('User/index'));
        $this->smarty->assign('title','找回密码');
        $this->smarty->display("User_v2/forget.html");
    }
    //重置密码
    public function forgetPassword(){
        $phone = I("get.phone", null, "strip_tags");
        $code = I("get.code", null, "strip_tags");
        $password = I("get.password", null, "strip_tags");

        $data = UserModel::resetUserPasswordByPhone($phone, $password, $code);
        $this->ajaxReturn($data);
    }


    private function bind_czk(){
        $backURL = UtilsModel::getFullDomain().U('user/index');
        $backURL=urlencode($backURL);
        redirect(C('BIND_URL').$backURL);
        die;
        $url = I("get.url",U('user/index'),'urldecode');
        $status = I("get.status",0, 'intval');
        if(!session("uid")) $url ? $url : redirect(U('User/index'));
        //if(session("bindPhone")) $url ? $url : redirect(U('User/index'));
        if(session("bindPhone") && !$status){
            $this->smarty->assign('title','手机改绑');
            $this->smarty->assign('status','0');
        }else{
            $this->smarty->assign('title','绑定手机');
            $this->smarty->assign('status','1');
        }
        //查询用户信息完善状态
        $status = UserModel::isUserInfoPerfect(session("uid"),OPT_BIND_PHONE);
        $this->smarty->assign('isBindPhone',$status);

        $this->smarty->assign('url',$url);
        $this->smarty->display("User_JCH/bind.html");
    }


    /**
     * 绑定手机号
     */
    public function bind(){
        $url = I("get.url",U('user/index'),'urldecode');
        $status = I("get.status",0, 'intval');
        if(!session("uid")) $url ? $url : redirect(U('User/index'));
        if(session("bindPhone") && !$status){
            $this->smarty->assign('title','手机改绑');
            $this->smarty->assign('status','0');
        }else{
            $this->smarty->assign('title','绑定手机');
            $this->smarty->assign('status','1');
        }
        //查询用户信息完善状态
        $status = UserModel::isUserInfoPerfect(session("uid"),OPT_BIND_PHONE);
        $this->smarty->assign('isBindPhone',$status);

        $this->smarty->assign('url',$url);
        $this->smarty->display("User_v2/bind.html");
    }
    //绑定手机
    public function goBind(){
        $phone = I("get.phone", null, "strip_tags");
        $code = I("get.code", null, "strip_tags");
        $uid = session("uid");

        $data = UserModel::bindUserPassword($uid, $phone, $code);
        $data['firstBindPhone'] = 0;
        if($data['status'] == 0){
            M('tmp_babyinfo',null,DB_MAIN_CFG)->where("uid={$uid}")->setField('phone',$phone);

            self::resetUserSessions($data['data']);
            $switch = UserModel::isUserInfoPerfect($uid,OPT_BIND_PHONE);
            if($switch){
                //手机绑定加积分100
                UserModel::addUserValByAction($uid,OPT_BIND_PHONE);
                $data['firstBindPhone'] = 1;
            }
        }
        $this->ajaxReturn($data);
    }

    public function info(){
        UtilsModel::checkLogin();
        $uid = session('uid');

        $address = explode('.',session('address'));
        $this->smarty->assign('title','个人信息');
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        //绑定手机得积分显示
        $status = UserModel::isUserInfoPerfect($uid,OPT_BIND_PHONE);
        $this->smarty->assign('isInfoPerfect',$status);
        
        //获取省市区街道 排序
        if(session('province')){
            $region = include_once(WEB_ROOT.'/data/area_info/'.session('province').'.php');
            $sort = array();
            $sort[]=$region[session('province')]['sort'];
            if(session('city')) {
                $sort[] = $region[session('city')]['sort'];
                if(session('district')){
                    $sort[] = $region[session('district')]['sort'];

                    if(session('street')){
                        $street_sort = $region[session('street')]['sort'];
                        $this->smarty->assign('street_sort',$street_sort);
                    }
                }
            }
            $this->smarty->assign('sort',$sort);
        }
        $this->smarty->assign('address',$address);
        $this->smarty->display("User_v2/info.html");
    }

    //更新用户信息
    public function update(){
        $uid = session('uid');
        $data = array('status'=>1,'msg'=>'参数错误');
        if(!$uid) $this->ajaxReturn($data);//数据为空返回列表
        $region = I('get.area',null,'trim');
        $address= I('get.address', '',   'strip_tags');
        $local = explode('/',$region);
        $data = array(
            'realName'  => I('get.realName',    session('realName') ,   'strip_tags')
            ,'nickname' => I('get.nickname',    session('nickname') ,   'strip_tags')
            ,'sex'      => I('get.sex',         session('sex') ,        'intval')
            ,'birthday' => I('get.birthday',    session('birthday') ,   'strip_tags')
            ,'province' => $local?$local[0]:session('province')
            ,'city'     => $local?$local[1]:session('city')
            ,'district' => $local?$local[2]:session('district')
            ,'address'  => $address,
            'deliverAddress'=>$address
        );

        
            $data = UserModel::update($uid,$data);
            $data['firstInfoPerfected'] = 0;
            if($data['status'] == 0){
                self::resetUserSessions($data['data']);
                //是否需要信息完善+积分功能
                $switch = UserModel::isUserInfoPerfect($uid,OPT_INFO_PERFECT);
                if($switch){
                    $address = explode('.',$data['data']['address']);
                    if(!$data['data']['nickname'] || !$data['data']['realName'] || !$data['data']['sex'] || !$data['data']['birthday']
                        || !$address[0] || !$address[1] || !$address[2] || !$address[3]){
                        //不处理
                    } else {
                        //信息完善加100积分
                        UserModel::addUserValByAction($uid, OPT_INFO_PERFECT);
                        $data['firstInfoPerfected'] = 1;
                    }
                }
            }
            $this->ajaxReturn($data);

    }

    //我的活动
    public function eventList(){
        UtilsModel::checkLogin();
        $uid = session('uid');
        $data = EventsModel::getMyEventsList($uid, 1, 8);
        $this->smarty->assign('title','我的报名');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('data',$data['data']);
        $this->smarty->display("User_v2/myEvent.html");
    }

    //我的发稿
    public function articleList(){
        UtilsModel::checkLogin();
        $uid = session('uid');
        $list = ArticleModel::getMyArticleList($uid);
        $this->smarty->assign('title','我的发稿');
        $this->smarty->assign('list',$list['data']);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->display("User_v2/myArticle.html");
    }

    //其他用户信息详情
    public function otherUser(){
        $uid= session('uid')+0;
        $fid = I('get.fid',0,'intval');
        if(!$fid){
            redirect($_SERVER['HTTP_REFERER']);//数据为空返回列表
        }
        $uinfo = UserModel::getUser($fid, 1);
        $list = ArticleModel::getMyArticleList($fid,'','',$uid);
//        $list = response('Article/getMyArticleList');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title','用户详情');
        $this->smarty->assign('uinfo',$uinfo['data']);
        $this->smarty->assign('list',$list['data']);
        $this->smarty->assign('followed',$list['followed']);
        $this->smarty->assign('followNum',$list['followNum']);
        $this->smarty->assign('city',explode('.',$uinfo['data']['address']));
        $this->smarty->display("OUser/index.html");


    }

    //用户投稿
    public function contribute(){
        UtilsModel::checkLogin();
        $uid=session('uid');
        $draftInfo=ArticleModel::getDraft($uid);
        $this->smarty->assign('obj',$draftInfo);
        $this->smarty->assign('picWidth',C("ARTICLE_PIC_WIDTH"));
        $this->smarty->assign('picHeight',C("ARTICLE_PIC_HEIGHT"));
        $this->smarty->assign('listPicWidth',C("ARTICLE_LIST_PIC_WIDTH"));
        $this->smarty->assign('listPicHeight',C("ARTICLE_LIST_PIC_HEIGHT"));
        $this->smarty->assign('title','投稿');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->display("Article/contribute.html");
    }

    /**
     * 保存投稿的草稿页
     * Author: shijy
     */
    public function saveDraft(){
        UtilsModel::checkLogin();
        $uid=session('uid');

        $title=I('post.title');
        $listPic=I('post.listPic');
        $content=I('post.content');
        foreach($content as $k=>$v){
            if(strlen($v)<=0){
                unset($content[$k]);
            }
        }
        $data['content']=implode(ArticleModel::CONTRIBUTE_SEPARATOR,$content);
        $data=ArticleModel::saveDraft($uid,$title,$listPic,$content);
        $this->ajaxReturn($data);
    }

    /**
     * 保存正式文件
     * Author: shijy
     */
    public function saveContribute(){
        UtilsModel::checkLogin();
        $uid=session('uid');
        $title=I('post.title','','strip_tags');
        $listPic=I('post.listPic');
        $contentRaw=I('post.content');

        $data=ArticleModel::saveContributeFromWAP($uid,$title,$listPic,$contentRaw);
        $this->ajaxReturn($data);

    }

    //我的收藏
    public function collection(){
        UtilsModel::checkLogin();
        $uid=session('uid');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $list = CollectModel::getMyCollection($uid,1,7);
        $this->smarty->assign('title','我的收藏');
        $this->smarty->assign('list',$list['data']);
        $this->smarty->display("User_v2/myCollection.html");
    }

    //我的收藏加载更多
    public function collectionMore(){
        $page = I('get.page',2,'intval');
        $uid = session('uid');
        $list = CollectModel::getMyCollection($uid,$page,7);
        $this->smarty->assign('list',$list['data']);
        $html = $this->smarty->fetch('User_v2/collectionMore.html');
        $this->ajaxReturn(array('status' =>0,'dataHtml' =>$html,'isPage' =>$list['nextPage']));
    }


    //我的粉丝
    public function follower(){
        UtilsModel::checkLogin();
        $uid=session('uid');
        $page=I('get.page',1,'intval');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $list = FollowModel::getMyFriendship($uid,1,$page,7);
        $this->smarty->assign('title','我的粉丝');
        $this->smarty->assign('list',$list['data']);
        $this->smarty->display("User_v2/myFollower.html");

    }

    //我的关注
    public function concern(){
        UtilsModel::checkLogin();
        $uid=session('uid');
        $page=I('get.page',1,'intval');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $list = FollowModel::getMyFriendship($uid,0,$page,7);
        $this->smarty->assign('title','我的关注');
        $this->smarty->assign('list',$list['data']);
        $this->smarty->display("User_v2/myConcern.html");
    }

    //我的大转盘奖品
    public function luckyPrize(){
        UtilsModel::checkLogin();
        $uid=session('uid')+0;
        $type = I('get.type',2,'int');
        $data = UserModel::getLuckyPrizeLog($uid,$type,1,C('LIST_PAGE_SIZE'));
        $this->smarty->assign('data',$data['data']);
        $this->smarty->assign('type',$type);
        $this->smarty->assign('isPage',$data['nextPage']);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title',"我的奖品");
        $this->smarty->display('User_v2/myPrize.html');
    }

    public function luckyPrizeMore(){
        UtilsModel::checkLogin();
        $uid = session('uid')+0;
        $page = I('post.page',2,'int');
        $type = I('post.type',2,'int');
        $data = UserModel::getLuckyPrizeLog($uid,$type,$page,C('LIST_PAGE<SIZE'));
        $this->smarty->assign('data',$data['data']);
        $html = $this->smarty->fetch('User_v2/myPrizeMore.html');
        $this->ajaxReturn(array('data'=>$data['data'],'html'=>$html,'isPage'=>$data['nextPage']));
    }

    //文章/活动 每日首次分享加积分、加喜爱指数
    public function weixinDayFirstShare(){
        UtilsModel::checkLogin();
        $uid = session('uid')+0;
        $objType = I('get.objType',0,'int');
        $objID = I('get.objID',0,'int');
   
        if(!$objType || !$objID){
            //暂无操作
            $this->ajaxReturn(array('status' => 1, 'msg' => '参数错误，请稍候再试'));
        }
        //记录每次分享数据
        M('u_operation_log',null,DB_MAIN_CFG)->add(array('uid'=>$uid,'operation'=>OPT_WEIXIN_SHARE,'addTime'=>time()));
        //加积分
        $this->ajaxReturn(UserModel::addUserValByAction($uid,OPT_WEIXIN_SHARE));
        
    }
}